import { AttributeCategorys } from './attributeCategory';
let _AttributeCategorys = AttributeCategorys;
/**
 * 拷贝下面两行到 mock/index.js中,即可支持客户端虚拟数据、免服务端连接
 * import AttributeCategoryMock from './mallm/pub/attributeCategoryMock';
 * AttributeCategoryMock.bootstrap(mock);
 */
export default {
  /**
   * mock bootstrap
   */
  bootstrap(mock) {    
  
    //获取列表 模糊查询、根据关键字查询
    mock.onGet('/mallm/pub/attributeCategory/listKey').reply(config => {
      let params = config.params;
      let mockAttributeCategorys = _AttributeCategorys.filter(attributeCategory => {
      	if(params.id||params.id==''){ if( attributeCategory.id.indexOf(params.id)>=0) return true;}
      	if(params.name||params.name==''){ if( attributeCategory.name.indexOf(params.name)>=0) return true;}
      	if(params.enabled||params.enabled==''){ if( attributeCategory.enabled.indexOf(params.enabled)>=0) return true;}
      	if(params.deptid||params.deptid==''){ if( attributeCategory.deptid.indexOf(params.deptid)>=0) return true;}
        return false;
      });
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve([200, {
            data: mockAttributeCategorys
          }]);
        }, 1000);
      });
    });
    
    

    //获取列表（分页/不分页）
    mock.onGet('/mallm/pub/attributeCategory/list').reply(config => {
      let params = config.params;
      let mockAttributeCategorys = _AttributeCategorys.filter(attributeCategory => {
      	var isTrue=false;
      	if(params.id||params.id==''){if(attributeCategory.id.indexOf(params.id)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.name||params.name==''){if(attributeCategory.name.indexOf(params.name)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.enabled||params.enabled==''){if(attributeCategory.enabled.indexOf(params.enabled)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.deptid||params.deptid==''){if(attributeCategory.deptid.indexOf(params.deptid)>=0){isTrue=true;}else{isTrue=false;}}
      	return isTrue;
	  });
      if(!config.params.currentPage){ 
	      return new Promise((resolve, reject) => {
	        setTimeout(() => {
	          resolve([200, {
	            data: mockAttributeCategorys
	          }]);
	        }, 1000);
	      });
      }
    
      let {pageSize,currentPage, total} = config.params;
      total = mockAttributeCategorys.length;
      mockAttributeCategorys = mockAttributeCategorys.filter((attributeCategory, index) => index < pageSize * currentPage && index >= pageSize * (currentPage - 1));
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve([200, {
            pageInfo: {
            	total:total
            },
            data: mockAttributeCategorys,
            tips:{
            	isOk:true,
            	msg:'查询成功'
            }
          }]);
        }, 1000);
      });
    });

    //删除
    mock.onPost('/mallm/pub/attributeCategory/del').reply(config => {
      var params = JSON.parse( config.data );
      let {  id  } = params;
      _AttributeCategorys = _AttributeCategorys.filter(attributeCategory => attributeCategory.id !== id);
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve([200, {
            tips:{
            	isOk:true,
            	msg:'删除成功'
            }
          }]);
        }, 500);
      });
    });

    //批量删除
    mock.onPost('/mallm/pub/attributeCategory/batchDel').reply(config => {
      var params = JSON.parse( config.data );
      _AttributeCategorys = _AttributeCategorys.filter(attributeCategory => !params.includes(attributeCategory.id));
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve([200, {
           tips:{
            	isOk:true,
            	msg:'删除成功'
            }
          }]);
        }, 500);
      });
    });

    //编辑
    mock.onPost('/mallm/pub/attributeCategory/edit').reply(config => {
      var params = JSON.parse( config.data );
      let { id,name,enabled,deptid } = params;
      _AttributeCategorys.some(attributeCategory => {
        if (attributeCategory.id === id) {
			attributeCategory.id = id;
			attributeCategory.name = name;
			attributeCategory.enabled = enabled;
			attributeCategory.deptid = deptid
          return true;
        }
      });
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve([200, {
            tips:{
            	isOk:true,
            	msg:'编辑成功'
            }
          }]);
        }, 500);
      });
    });

    //新增
    mock.onPost('/mallm/pub/attributeCategory/add').reply(config => {
      var params = JSON.parse( config.data );
      let { id,name,enabled,deptid } = params;
      _AttributeCategorys.push({
		id:id,
		name:name,
		enabled:enabled,
		deptid:deptid
      });
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve([200, {
            tips:{
            	isOk:true,
            	msg:'新增成功'
            }
          }]);
        }, 500);
      });
    });

  }
};